package com.demo.base.utils;

import org.springframework.core.GenericTypeResolver;
import org.springframework.util.Assert;

/**
 * Created by ethan on 2017/9/15.
 */
public abstract class GenericUtils {

    public static <T> Class<T> getTypeArgument(Class<?> clazz, Class<?> genericIfc, int argIndex) {
        Class<?>[] typeArguments = GenericTypeResolver.resolveTypeArguments(clazz, genericIfc);

        Assert.notNull(typeArguments, "未能获取到类型参数");
        Assert.state(typeArguments.length >= argIndex + 1, "未能获取到指定index的类型参数");

        //noinspection unchecked
        return (Class<T>) typeArguments[argIndex];
    }
}
